<?php



namespace App\Services;


use App\Models\Department;
use App\Service\Service;
use Dingo\Api\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Validation\Rule;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Class DepartmentService
 * @property integer $parent_id
 * @property string $name
 * @package App\Services
 * @author thanatos thanatos915@163.com
 */
class DepartmentService extends Service
{

    /**
     * @param Request $request
     * @author thanatos thanatos915@163.com
     */
    public static function search(Request $request)
    {
        // 获取所有部门
        $data = Department::get();
        $res = static::displayTree($data, 0);
        return $res;
    }

    /**
     * 部门详情
     * @param $id
     * @return Department|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
     * @author thanatos thanatos915@163.com
     */
    public static function show($id)
    {
        $data = Department::find($id);
        if (empty($data)) {
            throw new BadRequestHttpException("部门不存在");
        }
        return $data;
    }

    /**
     * 创建部门
     * @param Request $request
     * @param int $id
     * @return Department|\Illuminate\Database\Eloquent\Model
     * @author thanatos thanatos915@163.com
     */
    public static function createAndUpdate(Request $request, $id = 0)
    {
        $service = static::loadParams($request, [
            'name' => [
                'required',
                'string',
//                Rule::unique('department')->ignore($id)
            ],
            'parent_id' => 'numeric',
        ]);

        if ($service->parent_id > 0 && !Department::whereKey($service->parent_id)->exists()) {
            throw new BadRequestHttpException("上级部门不存在");
        }

        return Department::create([
            'name' => $service->name,
            'parent_id' => $service->parent_id ?: 0
        ]);
    }

    /**
     * 删除部门
     * @param $id
     * @throws \Exception
     * @author thanatos thanatos915@163.com
     */
    public static function delete($id)
    {
        $data = Department::whereId($id)->first();
        if (empty($data)) {
            throw new NotFoundHttpException("部门不存在");
        }

        if (Department::whereParentId($id)->exists()) {
            throw new BadRequestHttpException("当前部门存在下级部门，不能删除");
        }

        $data->delete();
    }

    /**
     * 返回部门的树形结构
     * @param Collection $data
     * @param $id
     * @return array
     * @author thanatos thanatos915@163.com
     */
    private static function displayTree($data, $id)
    {
        $tree = [];
        foreach ($data->where('parent_id', $id) as $item) {
            $tmp = [
                'title' => $item->name,
                'key' => $item->id,
                'value' => $item->id,
            ];
            if ($data->where('parent_id', $item->id)->count() > 0) {
                $tmp['children'] = static::displayTree($data, $item->id);
            }
            array_push($tree, $tmp);
        }
        return $tree;
    }

}
